﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace TestGenerator
{
    class TestGenerator
    {
        public static Dictionary<string, Question> ReadInputHTMLFile(string filename)
        {
            StreamReader reader = new StreamReader(filename, Encoding.GetEncoding("windows-1251"));
            string text = String.Empty;
            using (reader)
            {
                text = reader.ReadToEnd();
            }

            Dictionary<string, Question> database = CreateQuestionDatabase(text);
            ValidateDatabase(database);
            return database;
        }

        public static void ValidateDatabase(Dictionary<string, Question> database)
        {
            List<string> toBeRemoved = new List<string>();
            foreach (Question question in database.Values)
            {
                if (!question.HasCorrectAnswer() || !question.HasWrongAnswers())
                {
                    toBeRemoved.Add(question.QuestionText);
                }
            }
            foreach (string incompleteQuestionText in toBeRemoved)
            {
                Question question = database[incompleteQuestionText];
                if (!question.HasCorrectAnswer())
                {
                    Console.WriteLine("Question \"{0}\" does not have a correct answer and is going to be deleted.", incompleteQuestionText);
                }
                if (!question.HasWrongAnswers())
                {
                    Console.WriteLine("Question \"{0}\" does not have a wrong answer and is going to be deleted.", incompleteQuestionText);
                }
                database.Remove(incompleteQuestionText);
            }

        }

        private static Dictionary<string, Question> CreateQuestionDatabase(string text)
        {
            Dictionary<string, Question> questions = new Dictionary<string, Question>();
            string[] paragraphs = GetQuestionParagraph(text);
            foreach (string questionParagraph in paragraphs)
            {
                string questionText = GetQuestionText(questionParagraph);
                Question currentQuestion;
                if (!questions.ContainsKey(questionText))
                {
                    currentQuestion = new Question(questionText);
                    questions.Add(questionText, currentQuestion);
                }
                else
                {
                    currentQuestion = questions[questionText];
                }

                List<string> wrongAnswers = GetWrongAnswers(questionParagraph);
                foreach (string wrongAnswer in wrongAnswers)
                {
                    currentQuestion.AddWrongAnswer(wrongAnswer);
                }

                List<string> correctAnswers = GetCorrectAnswers(questionParagraph);
                foreach (string correctAnswer in correctAnswers)
                {
                    currentQuestion.AddCorrectAnswer(correctAnswer);
                }
            }

            return questions;
        }

        //public static List<string> GetQuestionParagraph(string text)
        //{
        //    string paragraphPattern = @"<p class=Question>s*((.|\n)*?)</p>";
        //    Match paragraphMatch = Regex.Match(text, paragraphPattern);
        //    List<string> paragraph = new List<string>();
        //    while (paragraphMatch.Success)
        //    {
        //        paragraph.Add(paragraphMatch.Value);
        //        paragraphMatch = paragraphMatch.NextMatch();

        //    }
        //    return paragraph;
        //}

        //public static string GetQuestionText(string paragraph)
        //{
        //    string questionPattern = @"<p class=Question>Question\.s*((.|\n)*?)</p>";
        //    Match questionMatch = Regex.Match(, paragraphPattern);
        //}

        public static string[] GetQuestionParagraph(string text)
        {
            string questionPattern = @"<p class=Question";
            string[] paragraph = Regex.Split(text, questionPattern);
            //       Console.WriteLine(paragraph[1]);
            return paragraph;
        }

        public static string GetQuestionText(string paragraph)
        {
            string questionTextPattern = @">Question.\s*((.|\n)*?)</p>";
            Match questionMatch = Regex.Match(paragraph, questionTextPattern);
            string questionText = questionMatch.Groups[1].Value;
            return questionText;
        }

        public static List<string> GetWrongAnswers(string paragraph)
        {

            List<string> wrongAnswerTexts = new List<string>();


            string wrongAnswerTextPattern = @"<p class=WrongAnswer>Wrong.\s*((.|\n)*?)</p>";
            Match wrongAnswerMatch = Regex.Match(paragraph, wrongAnswerTextPattern);
            while (wrongAnswerMatch.Success)
            {
                //          Console.WriteLine(wrongAnswerMatch.Groups[1].Value);
                wrongAnswerTexts.Add(wrongAnswerMatch.Groups[1].Value);
                wrongAnswerMatch = wrongAnswerMatch.NextMatch();
            }


            return wrongAnswerTexts;
        }

        //public static List<string> GetCorrectAnswers(string paragraph)
        //{
        //    string correctAnswerPattern = @"<p class=CorrectAnswer>";
        //    string[] correctAnswers = Regex.Split(paragraph, correctAnswerPattern);
        //    List<string> correctAnswerTexts = new List<string>();
        //    foreach (string current in correctAnswers)
        //    {
        //        string correctAnswerTextPattern = @"Correct.\s*((.|\n)*)</p>";
        //        Match correctAnswerMatch = Regex.Match(current, correctAnswerTextPattern);
        //        correctAnswerTexts.Add(correctAnswerMatch.Groups[1].Value);
        //    }
        //    return correctAnswerTexts;
        //
        //    return new List<string>();
        //}
        public static List<string> GetCorrectAnswers(string paragraph)
        {

            List<string> correctAnswerTexts = new List<string>();


            string correctAnswerTextPattern = @"<p class=CorrectAnswer>Correct.\s*((.|\n)*?)</p>";
            Match correctAnswerMatch = Regex.Match(paragraph, correctAnswerTextPattern);
            while (correctAnswerMatch.Success)
            {
                //        Console.WriteLine(correctAnswerMatch.Groups[1].Value);
                correctAnswerTexts.Add(correctAnswerMatch.Groups[1].Value);
                correctAnswerMatch = correctAnswerMatch.NextMatch();
            }
            return correctAnswerTexts;
        }

        static void Main(string[] args)
        {
            Console.SetOut(new StreamWriter("out.txt"));
            string filename = "DotNet-Test-2005-Questions.html";
            Dictionary<string, Question> database = ReadInputHTMLFile(filename);
            Console.WriteLine("{0} записа в базата.", database.Count);
            Test t = new Test(database);
            t.GenerateTest(1);
            Test t2 = new Test(database);
            t2.GenerateTest(2);
            Test t3 = new Test(database);
            t3.GenerateTest(3);
            Test t4 = new Test(database);
            t4.GenerateTest(4);
            Test t5 = new Test(database);
            t5.GenerateTest(5);
            t.GenerateAnswers();

        } 
    }
}
